/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.geekchique.runningbuddy.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.widget.Toast;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.activity.StopClock;
import com.geekchique.runningbuddy.controller.StopClockControllerImpl;

public class TimeRunnerService extends Service
{
    private NotificationManager mNM;
	Intent intent;


	@Override
    protected void onCreate() {

		mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
        intent = new Intent();
        intent.setClass(this, StopClockControllerImpl.class);

        Toast.makeText(this, getText(R.string.local_service_started), 2);
        addIconToServiceTray(R.drawable.stat_sample,  R.string.local_service_label, false);
     
    }	
	
    private void addIconToServiceTray(int moodId, int textId, boolean showTicker) {
        Intent contentIntent = new Intent(this, StopClock.class);
        CharSequence text = getText(textId);
        String tickerText = showTicker ? getString(textId) : null;

        mNM.notify(
                   999999, // we use a string id because it is a unique
                                                      // number.  we use it later to cancel the
                                                      // notification
                   new Notification(
                       this,                        // our context
                       moodId,                      // the icon for the status bar
                       tickerText,                  // the text to display in the ticker
                       System.currentTimeMillis(),  // the timestamp for the notification
                       "Jog Started",
                                                    // the title for the notification
                       text,                        // the details to display in the notification
                       contentIntent,               // the contentIntent (see above)
                       R.drawable.stat_sample,  // the app icon
                       "Running Buddy", // the name of the app
                       intent));                 // the appIntent (see above)
    }
	
    @Override
    protected void onDestroy() {
    	mNM.cancel(R.string.local_service_started);
    	Toast.makeText(this, getText(R.string.local_service_stopped), 2);
    	mNM.cancel(2);
      
    }


    private final IBinder mBinder = new Binder() {
        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
            return super.onTransact(code, data, reply, flags);
        }
    };


	@Override
	public IBinder onBind(Intent intent) {
        return mBinder;
	}
    
}

